package com.hdit.guimei.util;


import java.util.HashMap;
import java.util.Map;


/**
 * @author 冰镇小可乐
 * @ClassNameVerifyRegularUtil 验证表单的工具包
 * @Description TODO
 * @Date 2021/1/20 20:31
 * Version 1.0
 */
public class VerifyRegularUtil {

    //邮箱
    private static String  EMAIL = "^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$";

    //电话
    private static String PHONE= "^(1)[0-9]{10}$";

    //邮编
    private static String YZBM= "^\\d{6}$";

    //不允许为空
    public static String notNull = ".*[^ ].*";

    //用户名
    public static String userName="^[a-zA-Z][a-zA-Z0-9_]{4,15}$";

    //密码
    public static String Pwd="^[a-zA-Z]\\w{5,17}$";
    /***
    * @Method 验证不能位空
    * @Author 冰镇小可乐
    * @params  [value]
    * @Return boolean
    * @Exception
    * @Date 2021/1/25 14:30
    */
    public static boolean NotNull(String value){
        return value.matches(notNull);
    }
    /***
    * @Method 验证用户名 用户名以字母开头,长度在5-16之间,允许字母数字下划线
    * @Author 冰镇小可乐
    * @params  [username]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 14:18
    */
    public static boolean UserName(String username){
        return username.matches(userName);
    }
    /***
    * @Method 验证密码 密码以字母开头,长度在6-18之间,允许字母数字下划线
    * @Author 冰镇小可乐
    * @params  [pwd]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 14:28
    */
    public static  boolean PassWord(String pwd){
        return pwd.matches(Pwd);
    }
    /***
    * @Method 验证邮箱
    * @Author 冰镇小可乐
    * @params  [email]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 10:30
    */
    public static boolean Email(String email){
        return email.matches(EMAIL);
    }
    /***
    * @Method 验证电话号
    * @Author 冰镇小可乐
    * @params  [phone]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 10:31
    */
    public static boolean Phone(String phone){
        return phone.matches(PHONE);
    }
    /***
    * @Method 验证邮政编码
    * @Author 冰镇小可乐
    * @params  [yzbm]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 10:34
    */
    public static boolean Yzbm(String yzbm){
        return yzbm.matches(YZBM);
    }

    /***
    * @Method 验证身份证是否符合第一代和第二代标准
    * @Author 冰镇小可乐
    * @params  [cardcode]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 10:12
    */
    public static boolean cardCodeVerifySimple(String cardcode) {
        //第一代身份证正则表达式(15位)
        String isIDCard1 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
        //第二代身份证正则表达式(18位)
        String isIDCard2 ="^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$";
        //验证身份证
        if (cardcode.matches(isIDCard1) || cardcode.matches(isIDCard2)) {
            return true;
        }
        return false;
    }
    /***
    * @Method 验证身份证是否符合国家标准
    * @Author 冰镇小可乐
    * @params  [cardcode]
    * @Return boolean
    * @Exception  
    * @Date 2021/1/25 10:16
    */
    public static boolean cardCodeVerify(String cardcode) {
        int i = 0;
        String r = "error";
        String lastnumber = "";

        i += Integer.parseInt(cardcode.substring(0, 1)) * 7;
        i += Integer.parseInt(cardcode.substring(1, 2)) * 9;
        i += Integer.parseInt(cardcode.substring(2, 3)) * 10;
        i += Integer.parseInt(cardcode.substring(3, 4)) * 5;
        i += Integer.parseInt(cardcode.substring(4, 5)) * 8;
        i += Integer.parseInt(cardcode.substring(5, 6)) * 4;
        i += Integer.parseInt(cardcode.substring(6, 7)) * 2;
        i += Integer.parseInt(cardcode.substring(7, 8)) * 1;
        i += Integer.parseInt(cardcode.substring(8, 9)) * 6;
        i += Integer.parseInt(cardcode.substring(9, 10)) * 3;
        i += Integer.parseInt(cardcode.substring(10,11)) * 7;
        i += Integer.parseInt(cardcode.substring(11,12)) * 9;
        i += Integer.parseInt(cardcode.substring(12,13)) * 10;
        i += Integer.parseInt(cardcode.substring(13,14)) * 5;
        i += Integer.parseInt(cardcode.substring(14,15)) * 8;
        i += Integer.parseInt(cardcode.substring(15,16)) * 4;
        i += Integer.parseInt(cardcode.substring(16,17)) * 2;
        i = i % 11;
        lastnumber =cardcode.substring(17,18);
        if (i == 0) {
            r = "1";
        }
        if (i == 1) {
            r = "0";
        }
        if (i == 2) {
            r = "x";
        }
        if (i == 3) {
            r = "9";
        }
        if (i == 4) {
            r = "8";
        }
        if (i == 5) {
            r = "7";
        }
        if (i == 6) {
            r = "6";
        }
        if (i == 7) {
            r = "5";
        }
        if (i == 8) {
            r = "4";
        }
        if (i == 9) {
            r = "3";
        }
        if (i == 10) {
            r = "2";
        }
        if (r.equals(lastnumber.toLowerCase())) {
            return true;
        }
        return false;
    }
    /***
    * @Method 身份证验证
    * @Author 冰镇小可乐
    * @params  [cardId]
    * @Return java.util.Map
    * @Exception  
    * @Date 2021/1/25 10:09
    */
    public static Map isIdCardNo(String cardId){
        Map error=new HashMap();
        if (cardId.length()==15||cardId.length()==18){
            if (!VerifyRegularUtil.cardCodeVerifySimple(cardId)){
                error.put("meg","15位或18位身份证号码不正确");
                error.put("code","500");
                error.put("isOk","false");
                return error;
            }else {
                if (cardId.length()==18&&!VerifyRegularUtil.cardCodeVerify(cardId)){
                    error.put("msg","18位身份证号码不符合国家");
                    error.put("code","500");
                    error.put("isOk","false");
                    return error;
                }
            }
         }else{
                error.put("msg","身份证长度必须等于15位或者18位");
                error.put("code","500");
                error.put("idOk","false");
        }
        error.put("idOK","false");
        return error;
    }

}
